package net.godcode.olivenotes.components;

import java.util.Collection;
import java.util.Iterator;

import net.godcode.olivenotes.entities.Tag;
import net.godcode.t5c.mixins.RenderNode;

import org.apache.tapestry.ComponentResources;
import org.apache.tapestry.Link;
import org.apache.tapestry.MarkupWriter;
import org.apache.tapestry.annotations.BeginRender;
import org.apache.tapestry.annotations.Mixin;
import org.apache.tapestry.annotations.Parameter;
import org.apache.tapestry.annotations.SupportsInformalParameters;
import org.apache.tapestry.corelib.mixins.DiscardBody;
import org.apache.tapestry.corelib.mixins.RenderInformals;
import org.apache.tapestry.ioc.annotations.Inject;

@SupportsInformalParameters
/**
 * PostTags exists simply to render a list of tags associated with a post.
 * 
 * @author Chris Lewis Jan 14, 2008 <chris@thegodcode.net>
 * @version $Id: PostTags.java 32 2008-01-22 15:14:31Z burningodzilla $
 */
public class PostTags {
	
	/** The tags to render. */
	@Parameter(required = true)
	private Collection<Tag> tags;
	
	/**
	 * The logical page name that will be the target of the generated links.
	 */
	@Parameter(value = "literal:blog/tags")
	private String page;
	
	@Inject
	private ComponentResources resources;
	
	@SuppressWarnings("unused")
	@Mixin
	private RenderNode renderNode;
	
	@SuppressWarnings("unused")
	@Mixin
	private DiscardBody discardBody;
	
	@SuppressWarnings("unused")
	@Mixin
	private RenderInformals renderInformals;
	
	@BeginRender
	boolean beginRender(MarkupWriter writer) {
		Iterator<Tag> tIt /* :-D */ = tags.iterator();
		while(tIt.hasNext()) {
			Object tag = tIt.next();
			Link link = resources.createPageLink(page, true, tag);
			writer.element("a", "href", link.toAbsoluteURI()).text(tag.toString());
			writer.end();
			if(tIt.hasNext()) {
				writer.write(", ");
			}
		}
		return false;
	}
	
}